<!-- 消息列表 -->
<!-- 消息列表 -->
<!-- 消息列表 -->
<template>
  <div>
    <myDivider name="消息列表"></myDivider>
    <!-- 顶部搜索 -->
    <div class="top">
      <el-select class="width1" v-model="keytype" placeholder="选择消息类型" @change="SouSuo">
        <el-option label="所有" value=""></el-option>
        <el-option label="公告" value="notice"></el-option>
        <el-option label="资讯" value="information"></el-option>
        <el-option label="公告审批" value="noticeApproval"></el-option>
        <el-option label="资讯审批" value="informationApproval"></el-option>
        <el-option label="入住审批" value="customerRoomApproval"></el-option>
        <el-option label="报修工单" value="order"></el-option>
      </el-select>
      <el-date-picker @change="SouSuo" value-format="yyyy-MM-dd HH:mm:ss" style="width: 250px;" v-model="typeTime"
        type="daterange" unlink-panels :range-separator="null" start-placeholder="开始日期" end-placeholder="结束日期">
      </el-date-picker>
      <el-input class="width1" v-model="keytitle" placeholder="输入标题查询"></el-input>
      <el-button type="primary" @click="SouSuo">搜索</el-button>
    </div>

    <!-- 表格列表 -->
    <div class="tableBox">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column align="center" prop="" label="标题" width="160">
          <template slot-scope="scope">
            <span>
              {{ scope.row.message ? scope.row.message.title : '-' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="" label="消息分类" width="200">
          <template slot-scope="scope">
            <span>
              {{
                scope.row.type == 'notice' ? '公告消息' :
                  scope.row.type == 'information' ? '资讯消息' :
                    scope.row.type == 'noticeApproval' ? '公告审批' :
                      scope.row.type == 'informationApproval' ? '资讯审批' :
                        scope.row.type == 'customerRoomApproval' ? '入住审批' :
                          scope.row.type == 'order' ? '报修工单' : '--'
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="" label="内容概要">
          <template slot-scope="scope">
            {{ scope.row.message ? scope.row.message.pushSummary : '--' }}
          </template>

        </el-table-column>
        <el-table-column align="center" prop="" label="推送时间" width="170">
          <template slot-scope="scope">
            <span>
              {{ scope.row.createTime ? scope.row.createTime : '-' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="" label="推送账号" width="200">
          <template slot-scope="scope">
            <span>
              {{ scope.row.message ? scope.row.message.userName : '-' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="150">
          <template slot-scope="scope">
            <div style="display: flex;margin-top: 10px;justify-content:center;">
              <el-button size="small" @click="Preview(scope.row)" type="primary">详情</el-button>
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="roomName" label="房间信息" width="200">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" :content="scope.row.roomName" placement="top-start">
              <span> {{
                scope.row.roomName.length > 8 ? '...' + scope.row.roomName.slice(scope.row.roomName.length - 8) :
                scope.row.roomName
              }}</span>
            </el-tooltip>
          </template> 
        </el-table-column> -->
      </el-table>
    </div>

    <!-- 分页器 -->
    <myPagination :pageNum="pageNum" :total="total" @PageChange="PageChange" @pageSizeChange="pageSizeChange">
    </myPagination>
    <!-- 所有弹窗 -->
    <!--预览 -->
    <div>
      <el-dialog title="预览" :visible.sync="YuLanShow" width="650px">
        <el-form ref="YuLanform" :rules="YuLanRules" :model="YuLanData" label-width="90px">
          <div style="display: flex;">
            <el-form-item label="标题">
              <el-input disabled v-model="YuLanData.d1"></el-input>
            </el-form-item>
            <el-form-item label="消息分类">
              <el-input disabled v-model="YuLanData.d2"></el-input>
            </el-form-item>
          </div>
          <div style="display: flex;">
            <el-form-item label="推送主体">
              <el-input disabled v-model="YuLanData.d3"></el-input>
            </el-form-item>
            <el-form-item label="推送时间">
              <el-input disabled v-model="YuLanData.d4"></el-input>
            </el-form-item>
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="popClose('预览')">取 消</el-button>
          <el-button type="primary" @click="popOk('预览')">进入审核页面</el-button>
        </div>
      </el-dialog>
    </div>
    <!--消息详情 -->
    <div>
      <el-dialog title="预览" :visible.sync="XXXQShow" width="650px">
        <el-form ref="XXXQform" :rules="XXXQRules" :model="XXXQData" label-width="90px">
          <div style="display: flex;">
            <el-form-item label="标题" style="width: 350px;">
              <el-input disabled v-model="XXXQData.d1"></el-input>
            </el-form-item>
            <el-form-item label="消息分类" style="width: 350px;">
              <el-input disabled v-model="XXXQData.d2"></el-input>
            </el-form-item>
          </div>
          <div style="display: flex;">
            <el-form-item label="推送主体" style="width: 350px;">
              <el-input disabled v-model="XXXQData.d3"></el-input>
            </el-form-item>
            <el-form-item label="推送时间" style="width: 350px;">
              <el-input disabled v-model="XXXQData.d4"></el-input>
            </el-form-item>
          </div>
          <el-form-item label="内容" style="width: 610px;">
            <el-input disabled type="textarea" :rows="5" v-model="XXXQData.d5"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="popClose('消息详情')">取 消</el-button>
          <el-button type="primary" @click="popOk('消息详情')">确 认</el-button>
        </div>
      </el-dialog>
    </div>

  </div>
</template>

<script>
export default {

  data() {
    return {

      //顶部筛选
      keytype: '',
      typeTime: [],
      keytitle: '',
      // 表格数据
      tableData: [{
      }],
      // 预览弹窗
      YuLanShow: false,
      YuLanRules: {},
      YuLanData: {},
      //消息详情弹窗
      XXXQShow: false,
      XXXQRules: {},
      XXXQData: {},
      // 分页器
      total: 1,
      pageNum: 1,
      pageSize: 10,



    }
  },
  async activated() {

    this.keytype = ''
    this.typeTime = ['', '']
    this.keytitle = ''
    this.getTableList()
  },
  deactivated() {

  },
  methods: {
    handleChange(e) {
      console.log(e, this.values, '级联');

    },
    getChinese(val) { //获取推送内容的文字
      function isJson(str) { //判断是否是json格式
        try {
          JSON.parse(str);
          return true;
        } catch (e) {
          return false;
        }
      }
      function extractChineseCharacters(str) { //去除html标签  获取文字
        // 去除HTML标签  
        const noHtml = str.replace(/<[^>]*>/g, '');
        // 匹配汉字的正则表达式（包括基本汉字和扩展汉字）  
        const chineseRegex = /[\u4e00-\u9fff\u3400-\u4dbf\u20000-\u2a6df\u2a700-\u2b73f\u2b740-\u2b81f\u2b820-\u2ceaf\uf900-\ufaff\u2f800-\u2fa1f]/g;
        // 提取所有匹配的汉字  
        const chineseCharacters = noHtml.match(chineseRegex) || [];
        // 将数组中的汉字连接成一个字符串  
        const chineseCharactersStr = chineseCharacters.join('');
        return chineseCharactersStr;
      }
      if (isJson(val)) {
        let htmlString = JSON.parse(val)
        let start = 'class="boxTop">'
        let index1 = htmlString.indexOf('class="boxTop">')
        let index2 = htmlString.indexOf('<div class="boxBottom"')
        if (index1 == -1 || index2 == -1) {
          return '-'
        }
        let data = htmlString.substring(index1 + start.length, index2)
        let chinese = extractChineseCharacters(data);

        return chinese.replace(/nbsp;/g, '');
      } else {
        return val
      }
    },
    SouSuo() {
      this.pageNum = 1
      this.getTableList()
    },
    pageSizeChange(e) {
      this.pageNum = 1
      this.pageSize = e
      this.getTableList()
    },
    PageChange(e) {
      this.pageNum = e
      this.getTableList()
    },


    Preview(row) { //表格处理按钮
      console.log(row, '预览数据');
      switch (row.type) {
        case 'order':

          this.$router.push('/workOrder/workDetail?id=' + row.message.id)
          break
        case 'notice':
        case 'information':
          if (row.message && row.message.pushDescription) {
            sessionStorage.setItem('PreviewData', row.message.pushDescription)
            if (this.$route.name == '公告资讯审批') {
              location.reload()
              return
            }
            this.$router.push('/Preview')
          } else {
            this.$message.error('缺少消息内容')
          }

          break
        case 'noticeApproval':
          if (this.$route.name == '公告审批') {
            location.reload()
            return
          }
          this.$router.push(`/webNotice/NoticeShenPi?id=${row.message.id}&name=审批`)
          break
        case 'informationApproval':
          if (this.$route.name == '资讯审批') {
            location.reload()
            return
          }
          this.$router.push(`/information/informationShenPi?id=${row.message.id}&name=详情`)
          break
        case 'customerRoomApproval':
          if (this.$route.name == '入住审批') {
            location.reload()
            return
          }
          this.$router.push('/approvalForCheckIn')
          break
      }
      // switch (row.type) {
      //   case "information": //资讯
      //     this.$router.push('/information/informationShenPiList')
      //     break
      //   case "notice": //公告
      //     this.$router.push('/webNotice/NoticeShenPiList')
      //     break
      // }
    },


    async getTableList() { // 获取表格列表
      if (!this.typeTime) {
        this.typeTime = ['', '']
      }
      let res = await this.$api.message.MywebMessagelist({
        "messageType": this.keytype, //消息类型: notice:公告 information:资讯 system:系统通知 选填
        "startTime": this.typeTime[0], //对receiveTime的筛选 选填
        "endTime": this.typeTime[1], //对receiveTime的筛选 选填
        "keyword": this.keytitle, //关键词
        "pageNum": this.pageNum,
        "pageSize": this.pageSize
      })
      if (res.data.list.length < 1 && this.pageNum != 1) { //判断，当搜索关键字有内容时，我直接点击分页，会造成关键词列表中没有某某页的数据，那么我就用第一页重新请求一次
        this.pageNum = 1
        this.getTableList()
        return
      }
      this.tableData = []
      res.data.list.forEach(el => {

        if (el.message) {
          this.tableData.push(el)
        }
      })
      this.total = res.data.total
    },
    popOk(val) { //所有弹窗的确认
      switch (val) {
        case '预览':
          this.YuLanShow = false
          break
        case '消息详情':
          this.XXXQShow = false
          break
      }
    },
    popClose(val) { //所有弹窗的取消
      switch (val) {
        case '预览':
          this.YuLanShow = false
          break
        case '消息详情':
          this.XXXQShow = false
          break
      }
    },
  },
}
</script>

<style lang="less" scoped>
.width1 {
  width: 200px;
}

.width2 {
  width: 500px;
}

.top {
  width: 100%;
  padding: 20px 30px;
  box-sizing: border-box;
  position: relative;
}

.tableBox {
  margin-top: 50px;

  /deep/ .el-table {
    .has-gutter {
      .is-leaf {}
    }
  }

}
</style>